[Django] CBVs vs FBVs

The most import takeaway from this post

CBVs and FBVs don’t replace each other

Comparison

CBVs FBVs
Code Flow Implicit Explicit
Readability Harder Easy
Reusability Great Hard
Specialized Functionality No but can use generic CBVs Handling of HTTP methods via conditional branching
Usage of Decorators Require extra import or method override Straightforward
Code Duplication DRY A lot
Extendability Easy using Mixins Hard

Conclusion

It’s hard to say which is better.

It highly depends on the context.

If you need to implement let’s say a list view, it’d be better with CBVs where you can just subclass the ListView and override the attributes.

On the other hand if you want to make a complicated operation or handle multiple forms at the same time, FBVs will be better.


Introduction to class-based views

from django.http import HttpResponse

def my_view(request):
    if request.method == 'GET':
        # <view logic>
        return HttpResponse('result')
from django.http import HttpResponse
from django.views import View

class MyView(View):
    def get(self, request):
        # <view logic>
        return HttpResponse('result')



Reference

Django : Class Based Views vs Function Based Views

https://docs.djangoproject.com/en/2.2/topics/class-based-views/intro/




ABOUT ME
I write codes and words.
제가 궁금하다면 ABOUT ME 버튼을 눌러보세요!

GitHubLinkedIn